package com.edu.marker.activities;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.edu.marker.R;
import com.edu.marker.R.color;
import com.edu.marker.pojo.ExamInfo;
import com.edu.marker.pojo.PaperInfo;
import com.edu.marker.pojo.QuestionInfo;
import com.edu.marker.task.WSAPI;
import com.edu.marker.task.WSAPI.WSACTION;
import com.edu.marker.task.WSTask.TaskListener;
import com.edu.marker.utils.Constants;
import com.edu.marker.utils.Prefs;
import com.edu.marker.utils.ToastUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class PaperInfoActivity extends Activity {
	private ProgressBar progressBar;
	private RelativeLayout tips_layout;
	private TextView titleTextView,userNameTextView,userIDTextView,
					examNameTextView,kemuTextView,totalNumTextView;
	private Button acceptBtn,refuseBtn;
	private PaperInfo paperInfo;
	private GridView mGridView;
	private ListView mListView;
	private TipsExamAdapter mTipsExamAdapter = null;
	private TipsQuestionAdapter mTipsQuestionAdapter = null;
	private ArrayList<String> mylist = new ArrayList<String>();
	public ArrayList<ExamInfo> mExamInfoList = null;
	public int clickPosition = -1;
	private String examID,questionID;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_tips);
		
		setupViews();
		
		final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("serverUrl", Constants.getExamInfoUrl));
		params.add(new BasicNameValuePair("method", "GetExamInfo"));
		
		WSAPI.doRequest(this, mTaskListener, params, WSACTION.ACT_PAPER);
	}
	
	public void setupViews(){
		mGridView = (GridView)findViewById(R.id.tips_exam_name_gridview);
		mListView = (ListView)findViewById(R.id.tips_exam_tihao_value);
//		mListView.setOnItemSelectedListener(new OnItemSelectedListener() {
//			@Override
//			public void onItemSelected(AdapterView<?> arg0, View arg1,
//					int postion, long arg3) {
//				if(postion != clickPosition){
//                    clickPosition = postion;
//                }
//                else{
//                    clickPosition = -1;
//                }
//				mTipsQuestionAdapter.notifyDataSetChanged();
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				
//			}
//		}); 
//		mListView.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1, int postion,
//					long arg3) {
//				if(postion != clickPosition){
//                    clickPosition = postion;
//                }
//                else{
//                    clickPosition = -1;
//                }
//				mTipsQuestionAdapter.notifyDataSetChanged();
//			}
//		});
		progressBar = (ProgressBar)findViewById(R.id.tips_progressBar);
		tips_layout = (RelativeLayout)findViewById(R.id.tips_layout);
		tips_layout.setVisibility(View.GONE);
		progressBar.setVisibility(View.VISIBLE);
		titleTextView = (TextView)findViewById(R.id.tips_title_text);
		String titleStr = Prefs.getStrValue(PaperInfoActivity.this, Constants.ACCOUNT,"");
		titleTextView.setText(titleStr);
		userNameTextView = (TextView)findViewById(R.id.tips_username_tv);
		String userStr = Prefs.getStrValue(PaperInfoActivity.this, Constants.USERNAME,"");
		userNameTextView.setText(userStr);
		userIDTextView = (TextView)findViewById(R.id.tips_userid_tv);
		examNameTextView = (TextView)findViewById(R.id.exam_name_tv);
		kemuTextView = (TextView)findViewById(R.id.tips_exam_kemu_value);
		totalNumTextView = (TextView)findViewById(R.id.tips_exam_zongshu_value);
		acceptBtn = (Button)findViewById(R.id.tips_button_accept);
		acceptBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if("".equals(examID)||examID==null){
					ToastUtils.failed(PaperInfoActivity.this,getString(R.string.login_faill_err));
				}else if("".equals(questionID)||questionID==null){
					ToastUtils.failed(PaperInfoActivity.this,getString(R.string.login_faill_err));
				}else{
					Intent intent = new Intent();
					intent.setClass(PaperInfoActivity.this, MarkingActivity.class);
					intent.putExtra("myExamID",examID);
					intent.putExtra("myQuestionID",questionID);
					PaperInfoActivity.this.startActivity(intent);
					}
			}
		});
		refuseBtn = (Button)findViewById(R.id.tips_button_refuse);
		refuseBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
			}
		});
	}
	
	private final TaskListener mTaskListener = new TaskListener(){
		@Override
		public void onSuccess(int action, Object result) {
			paperInfo = (PaperInfo) result;
//			paperInfo = initData();
			mExamInfoList = paperInfo.getExamInfoList();
			if(mExamInfoList.size()>0){
				int length = mExamInfoList.size();
				for(int i=0;i<length;i++){
				 	ExamInfo mExamInfo = mExamInfoList.get(i);
				 	mylist.add(mExamInfo.examName);
				}
			}
			mTipsExamAdapter = new TipsExamAdapter(PaperInfoActivity.this,mylist,myHandler);
			mGridView.setAdapter(mTipsExamAdapter);
			
			Message mesg = myHandler.obtainMessage(1);
			myHandler.sendMessage(mesg);
		}

		@Override
		public void onFailed(int action, String errcode, Exception ex) {
			ToastUtils.message(PaperInfoActivity.this, getString(R.string.tips_faill_err));
		}
	};

	public Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
			case 1:
				tips_layout.setVisibility(View.VISIBLE);
				progressBar.setVisibility(View.GONE);
				break;
			case 2:
				int postion = (Integer)msg.obj;
				ExamInfo mExamInfo = mExamInfoList.get(postion);
				examNameTextView.setText(mExamInfo.examName);
				kemuTextView.setText(mExamInfo.examName);
				examID = mExamInfo.examID;
				ArrayList<QuestionInfo> mQuestion = mExamInfo.question;
				int len = mQuestion.size();
				int totleNum = 0;
				for(int i=0;i<len;i++){
					QuestionInfo mQuestionInfo = mQuestion.get(i);
					
					totleNum = totleNum+Integer.valueOf(mQuestionInfo.questionCount);
				}
				mTipsQuestionAdapter = new TipsQuestionAdapter(PaperInfoActivity.this,mQuestion);
				mListView.setAdapter(mTipsQuestionAdapter);
				mListView.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
					}
				});
				totalNumTextView.setText(totleNum+"题");
				break;
			case 3:
				String vaule = (String)msg.obj;
				Log.d("hsj", "questionID的值为："+vaule);
//				questionID = vaule;
				questionID = vaule;
				break;
			}
		}
	};
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
	
	public class TipsQuestionAdapter extends BaseAdapter{
		private ArrayList<QuestionInfo> list = new ArrayList<QuestionInfo>();
		public LayoutInflater myLayoutInflater = null;
		
		public TipsQuestionAdapter(Context myContext,ArrayList<QuestionInfo> mylist){
			this.list = mylist;
			myLayoutInflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			
			if(convertView==null){
				convertView = myLayoutInflater.inflate(R.layout.tips_tihao_item, null);
				holder = new ViewHolder();
				holder.item_text = (TextView)convertView.findViewById(R.id.tips_exam_tihao_tvvalue);
				
				convertView.setTag(holder);
			}
			else{
				holder = (ViewHolder)convertView.getTag();
			}
//			if(position==clickPosition){
//				holder.item_text.setTextColor(color.green);
//			}
//			else{
//				holder.item_text.setTextColor(color.defulat);
//			}
			
			final QuestionInfo mQuestionInfo = list.get(position);
			String name = "第"+(position+1)+"题      "+mQuestionInfo.questionName+"      共"+
					mQuestionInfo.questionCount+"题      已阅"+
					mQuestionInfo.questionIsCheckedCount+"题";
			holder.item_text.setText(name);
			if(mQuestionInfo.states==true){
				holder.item_text.setTextColor(color.green);
			}
			
			convertView.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
//					clickPosition = position;
//					mposition = position;
					Log.d("hsj", "第"+position+"行被选中了。");
					Message msg = myHandler.obtainMessage(3,mQuestionInfo.questionID);
					myHandler.sendMessage(msg);
				}
			});
			return convertView;
		}
		
		class ViewHolder{
			TextView item_text;
		}
	}
}
